var myrequest = require('../../utils/util.js')
var router = require('../../utils/router.js')
var approuter = router.router
var app = getApp()
Page({
  data: {
    class_subject:"",
    class_teacher:"",
    class_adress:'',
    dateValue: '日期',
    starttimeValue: '开始时间',
    endtimeValue: '结束时间',
    class_intro:'',
    pickerHidden: true,
    chosen: '',
    /**提示图标 */
    subject_icon:'',
    teacher_icon:'',
    adress_icon:'',
    /** 发布地址 */
    publishAdress:'',
    /** 老师头像图片 */
    class_teacher_pic:'',
    /** 用户信息 */
    userInfo:{},
  },
  // 监听 主题 blur事件
  subjectBlur:function(e){
    this.setData({
      class_subject: e.detail.value.trim()
    })
    if (e.detail.value.trim()){
      this.setData({
        subject_icon:'success'
      })
    }else{
      this.setData({
        subject_icon: 'warn'
      })
    }
  },

  // 监听 老师 blur事件
  teacherBlur:function(e){
    this.setData({
      class_teacher:e.detail.value.trim()
    })
    if (e.detail.value.trim()) {
      this.setData({
        teacher_icon: 'success'
      })
    } else {
      this.setData({
        teacher_icon: 'warn'
      })
    }
  },

  // 监听 地点 事件
  adressBlur:function(e){
    this.setData({
      class_adress: e.detail.value.trim()
    })
    if (e.detail.value.trim()) {
      this.setData({
        adress_icon: 'success'
      })
    } else {
      this.setData({
        adress_icon: 'warn'
      })
    }
  },

  starttimePickerBindchange: function (e) {
    this.setData({
      starttimeValue: e.detail.value
    })
  },
  endtimePickerBindchange: function (e) {
    this.setData({
      endtimeValue: e.detail.value
    })
  },
  datePickerBindchange: function (e) {
    this.setData({
      dateValue: e.detail.value
    })
  },
  pickerConfirm: function (e) {
    this.setData({
      pickerHidden: true
    })
    this.setData({
      chosen: e.detail.value
    })
  },
  pickerCancel: function (e) {
    this.setData({
      pickerHidden: true
    })
  },
  pickerShow: function (e) {
    this.setData({
      pickerHidden: false
    })
  },
  formSubmit: function (e) {
    var that = this
    // 校验课程主题
    if (!this.data.class_subject){
      this.messageToast('请填写课程主题')
      return;
    }
    // 校验主讲老师
    if(!this.data.class_teacher){
      this.messageToast('请填写主讲老师')
      return;
    }
    // 校验地点
    if (!this.data.class_adress) {
      this.messageToast('请填写开课地点')
      return;
    }
    // 校验时间

    var startTimeHour = parseInt(this.data.starttimeValue.slice(0,2))
    var startTimeMin = parseInt(this.data.starttimeValue.slice(3))

    var endTimeHour = parseInt(this.data.endtimeValue.slice(0, 2))
    var endTimeMin = parseInt(this.data.endtimeValue.slice(3))

    console.log(startTimeHour+''+startTimeMin+''+endTimeHour+''+endTimeMin);
    if(startTimeHour > endTimeHour){
      this.messageToast("时间安排有误")
      this.setData({
        starttimeValue: '开始时间',
        endtimeValue: '结束时间'
      })
      return;
    } else if ((startTimeHour == endTimeHour) && (startTimeMin >= endTimeMin)){
      this.messageToast("时间安排有误")
      this.setData({
        starttimeValue: '开始时间',
        endtimeValue: '结束时间'
      })
      return;
    }
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    var sessionKey = wx.getStorageSync('LoginSessionKey');
    if (sessionKey != "" && sessionKey != null) {
      // 模拟Cookie
      var header = { 'content-type': 'application/json', 'Cookie': 'JSESSIONID=' + sessionKey }
      myrequest.sendRequest(that.data.publishAdress, 'POST', e.detail.value, header)
        .then(function (reponse) {
          // 成功调用
          if(reponse.data.Status == 'SUCCESS'){
            wx.redirectTo({
              url: '../../pages/templates/templates'
            })
          }
          console.log(reponse)
        }, function (error) {
          // 失败调用
          console.log(error)
        }
        )
    }
  

    // wx.navigateTo({
    //   url: '../../pages/templates/templates',
    // })
  },
  formReset: function (e) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
    this.setData({
      chosen: ''
    })
  },
  // 消息提示框
  messageToast:function(msg){
    wx.showToast({
      title: msg,
      icon: 'fail',
      duration: 2000
    })
  },
  onLoad:function(){
    var that = this
    console.log(router)
    that.setData({
      publishAdress: approuter.publish_class
    })
    console.log(router.router)
    wx.getUserInfo({
      withCredentials: false,
      success: function (res) {
        that.setData({
          userInfo:res.userInfo,
          class_teacher_pic:res.userInfo.avatarUrl
        })
        console.log(res.userInfo)
      }
    })
  }
})
